#include "page_manager.h"
#include <stdlib.h>
#include <string.h>
#include <limits.h>

// 页面管理器结构：负责管理页面分配和释放
struct page_manager {
    uint8_t* allocation_map;  // 页面分配位图，每一位表示一个页面的分配状态
    uint32_t max_pages;       // 最大页面数量
    uint32_t allocated_count; // 已分配页面数量
    page_id_t next_page_id;   // 下一个可分配的页面ID
};

// 创建页面管理器
// 返回值：
//   成功返回页面管理器指针，失败返回NULL
page_manager_t* page_manager_create() {
    page_manager_t* manager = malloc(sizeof(page_manager_t));
    if (!manager) {
        return NULL;
    }

    manager->max_pages = MAX_PAGES;
    manager->allocated_count = 0;
    manager->next_page_id = 0;

    // 页面分配位图，每一位表示一个页面的分配状态
    size_t bitmap_size = (manager->max_pages + 7) / 8;
    manager->allocation_map = calloc(bitmap_size, 1);
    if (!manager->allocation_map) {
        free(manager);
        return NULL;
    }

    return manager;
}

// 检查页面是否已分配
// 参数：
//   manager - 页面管理器指针
//   page_id - 要检查的页面ID
// 返回值：
//   1表示已分配，0表示未分配
int page_manager_is_allocated(page_manager_t* manager, page_id_t page_id) {
    if (page_id >= manager->max_pages) {
        return 0;
    }

    uint32_t byte_index = page_id / 8;
    uint32_t bit_index = page_id % 8;
    return (manager->allocation_map[byte_index] >> bit_index) & 1;
}

// 页面管理器销毁
void page_manager_destroy(page_manager_t* manager) {
    if (manager) {
        free(manager->allocation_map);
        free(manager);
    }
}

// 页面分配
page_id_t page_manager_allocate(page_manager_t* manager) {
    // 首先尝试从空闲的页面中分配
    for (page_id_t i = 0; i < manager->max_pages; i++) {
        if (!page_manager_is_allocated(manager, i)) {
            // 标记为已分配
            uint32_t byte_index = i / 8;
            uint32_t bit_index = i % 8;
            manager->allocation_map[byte_index] |= (1 << bit_index);
            manager->allocated_count++;
            return i;
        }
    }

    // 如果没有空闲的页面，分配新页面
    if (manager->next_page_id < manager->max_pages) {
        page_id_t page_id = manager->next_page_id++;
        uint32_t byte_index = page_id / 8;
        uint32_t bit_index = page_id % 8;
        manager->allocation_map[byte_index] |= (1 << bit_index);
        manager->allocated_count++;
        return page_id;
    }

    return UINT32_MAX; // 页面已满
}

// 新增：强制分配特定ID的页面
page_id_t page_manager_force_allocate(page_manager_t* manager, page_id_t desired_id) {
    if (desired_id >= manager->max_pages) {
        return UINT32_MAX; // ID超出范围
    }
    
    if (!page_manager_is_allocated(manager, desired_id)) {
        // 标记为已分配
        uint32_t byte_index = desired_id / 8;
        uint32_t bit_index = desired_id % 8;
        manager->allocation_map[byte_index] |= (1 << bit_index);
        manager->allocated_count++;
    }
    
    // 更新next_page_id以确保后续分配不会冲突
    if (desired_id >= manager->next_page_id) {
        manager->next_page_id = desired_id + 1;
    }
    
    return desired_id;
}

// 页面释放
storage_error_t page_manager_free(page_manager_t* manager, page_id_t page_id) {
    if (page_id >= manager->max_pages) {
        return STORAGE_ERROR_INVALID;
    }

    if (!page_manager_is_allocated(manager, page_id)) {
        return STORAGE_ERROR_NOT_FOUND;
    }

    // 页面释放
    uint32_t byte_index = page_id / 8;
    uint32_t bit_index = page_id % 8;
    manager->allocation_map[byte_index] &= ~(1 << bit_index);
    manager->allocated_count--;

    return STORAGE_OK;
}

// 页面释放
uint32_t page_manager_get_allocated_count(page_manager_t* manager) {
    return manager->allocated_count;
}